package com.ntschy.web.controller.bt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtPolicy;
import com.ntschy.bt.entity.dto.btPolicyDTO.BtPolicyAddUpdateDTO;
import com.ntschy.bt.entity.dto.btPolicyDTO.BtPolicyAuditPageDTO;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyAuditStatusEnum;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyLevelEnum;
import com.ntschy.bt.entity.enums.btpolicyEnum.BtPolicyTypeEnum;
import com.ntschy.bt.entity.mapstruct.BtPolicyMapstruct;
import com.ntschy.bt.service.IBtPolicyService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/bt/policy")
@Api(tags = "发布审核接口")
@Validated
public class BtPolicyAuditController extends BaseController {
    @Autowired
    private IBtPolicyService policyService;

    /**
     * 新增政策发布
     *
     * @param auditPageDto
     * @return
     */
    @ApiOperation(value = "发布审核列表", notes = "")
    @ApiImplicitParam(name = "auditPageDto", value = "发布审核列表", required = true, dataType = "BtPolicyPageDTO")
    @PostMapping("/pageWaitAuditPolicy")
    public AjaxPageResult pageWaitAuditPolicy(@RequestBody BtPolicyAuditPageDTO auditPageDto){
        Page<BtPolicy> page = PageHelper.startPage(auditPageDto.getCurrPage(), auditPageDto.getPageSize());
        BtPolicy queryBtPolicy = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.pageAduitBtPolicy(auditPageDto);
        List<BtPolicy> list = policyService.pageBtPolicyAudit(queryBtPolicy);
        return buildPageResult(list,page);
    }


    /**
     * 前端获取政策级别下拉框选项
     */
    @ApiOperation(value = "前端获取政策级别下拉框",notes = "")
    @PostMapping("/policyOptionInfo")
    public AjaxResult getPolicyOptionInfo(){
        AjaxResult levelOptions = new AjaxResult();
        List listOptions = new ArrayList();
        listOptions.add(BtPolicyLevelEnum.values());
        listOptions.add(BtPolicyTypeEnum.values());
        listOptions.add(BtPolicyAuditStatusEnum.values());
        levelOptions.setData(listOptions);
        return AjaxResult.success("获取成功",listOptions);
    }

    /**
     * 政策发布 政策审核详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "政策审核详情", notes = "")
    @ApiImplicitParam(name = "id", value = "政策审核详情", required = true, dataType = "BtPolicyPageDTO")
    @GetMapping("/fetchWaitAuditPolicy/{id}")
    public AjaxResult<BtPolicy> getAuditPolicyInfo(@PathVariable String id){
        return AjaxResult.success("查询成功",policyService.selectBtPolicyInfoById(id));
    }

    /**
     * 政策库 发布审核操作 1-退回 2-通过
     */
    @ApiOperation(value = "政策库 发布审核操作 1-退回 2-通过", notes = "")
    @ApiImplicitParam(name = "btPolicy", value = "政策库 发布审核操作 1-退回 2-通过", required = true, dataType = "BtPolicyPageDTO")
    @PostMapping("/updateAuditPolicy")
    public AjaxResult updateAduitPolicy(@RequestBody BtPolicyAddUpdateDTO btPolicy){
        BtPolicy policy = BtPolicyMapstruct.BT_POLICY_MAP_STRUCT.addUpdateDtoToDo(btPolicy);
        // if (btPolicy.getExamineResult().getValue() == BtPolicyAuditStatusEnum.TO_PAST.getValue()){
        //     btPolicy.setExamineResult(BtPolicyAuditStatusEnum.TO_BACK);
        // }
        return toAjax(policyService.updateAduitPolicy(policy),"审核成功","审核失败");
    }
}
